package ex0.expressions;

import ex0.Calc;
import ex0.expressions.operations.*;

/**
 * Created with IntelliJ IDEA.
 * User: Roi
 * Date: 10/18/13
 */
public class ExpressionFactoryImpl implements ExpressionFactory {

    private Calc.State state;

    public ExpressionFactoryImpl(Calc.State state) {
        this.state = state;
    }

    @Override
    public Expression createExpression(String token) {
        if (Character.isDigit(token.charAt(0))) {
            return new Number(Double.parseDouble(token));
        }
        else {
            Double secondVal = state.valueStack.pop();
            Double firstVal = state.valueStack.pop();
            String secondStr = state.infixExpStack.pop();
            String firstStr = state.infixExpStack.pop();

            if (token.equals("+")) {
                return new AddOperation(firstVal, secondVal, firstStr, secondStr);
            }
            else if (token.equals("-")) {
                return new SubtractOperation(firstVal, secondVal, firstStr, secondStr);
            }
            else if (token.equals("*")) {
                return new MultiplyOperation(firstVal, secondVal, firstStr, secondStr);
            }
            else if (token.equals("/")) {
                return new DivideOperation(firstVal, secondVal, firstStr, secondStr);
            }
            else if (token.equals("^")) {
                return new PowerOperation(firstVal, secondVal, firstStr, secondStr);
            }
            else {
                return null;
            }
        }
    }
}
